﻿using System;
using System.Collections.Generic;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.LookupLists
{
    public class BusinessObjectLookupListControl : UserControlWin, ILookupListControl
    {
        private DMProperty _dmProperty;
        private DMLookupListBusinessObject _lookupList;
        //private IBOComboBoxSelector _classSelector;
        //private IBOComboBoxSelector _sortByPropSelector;
        //private ICheckBox _ascendingCheckBox;
        //private ITextBox _criteriaTextBox;
        private readonly IPanelInfo _panelInfo;
        private SortPropertySelectorMapper _sortPropertySelectorMapper;
        private IComboBox _classComboBox;
        private LookupComboBoxMapper _classComboBoxMapper;

        public LookupComboBoxMapper ClassComboBoxMapper
        {
            get { return _classComboBoxMapper; }
        }

        private SortPropertySelectorMapper SortPropertySelectorMapper
        {
            get { return _sortPropertySelectorMapper; }
        }

        public IPanelInfo PanelInfo
        {
            get { return _panelInfo; }
        }

        public IControlFactory ControlFactory { get; set; }
        const string _businessObject = "Business Object";

        public BusinessObjectLookupListControl(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            ControlFactory = controlFactory;
            BorderLayoutManager borderLayoutManager = ControlFactory.CreateBorderLayoutManager(this);
            PanelBuilder panelBuilder = new PanelBuilder(ControlFactory);
            _panelInfo = panelBuilder.BuildPanelForForm(ClassDef.Get<DMLookupListBusinessObject>().UIDefCol["default"].UIForm);
            _sortPropertySelectorMapper = (SortPropertySelectorMapper)this._panelInfo.FieldInfos["DMSortOrder"].ControlMapper;
            _classComboBox = (IComboBox)this._panelInfo.FieldInfos["ClassID"].InputControl;
            _classComboBoxMapper = ((LookupComboBoxMapper)this._panelInfo.FieldInfos["ClassID"].ControlMapper);

            _panelInfo.BusinessObject = _lookupList;
            
            borderLayoutManager.AddControl(_panelInfo.Panel);
        }

        private void ClassUpdated(object sender, BOPropEventArgs e)
        {
            SetDMPropertiesOnSortPropertyMapper();

            //LookupComboBoxMapper sortPropertyMapper = (LookupComboBoxMapper) _panelInfo.FieldInfos["SortPropertyID"].ControlMapper;
            //Dictionary<string, string> displayValueDictionary = new Dictionary<string, string>();
            //if (dmClass != null)
            //{
            //    displayValueDictionary = BusinessObjectLookupList.CreateDisplayValueDictionary(dmClass.Properties,true, Convert.ToString);
            //    _lookupList.Assembly = dmClass.Assembly.AssemblyName;
            //}
            //sortPropertyMapper.LookupList = displayValueDictionary;
            //_sortByPropSelector.Enabled = true;
            //_criteriaTextBox.Enabled = true;
            //_sortByPropSelector.BusinessObjectCollection = ((DMClass)ClassSelector.SelectedBusinessObject).Properties;
            //if (_sortByPropSelector.SelectedBusinessObject != null)
            //{
            //    _ascendingCheckBox.Enabled = true;
            //}
        }

        public DMProperty Property
        {
            get { return _dmProperty; }
            set
            {
                _dmProperty = value;
                bool hasProperty = _dmProperty != null;
                if (hasProperty)
                {
                    DMLookupList lookupList = _dmProperty.LookupList;
                    _lookupList = lookupList is DMLookupListBusinessObject 
                        ? (DMLookupListBusinessObject)lookupList 
                        : new DMLookupListBusinessObject();
                    _lookupList.Props["ClassID"].Updated += ClassUpdated;
                    if (_lookupList.DMSortOrder == null)
                    {
                        _lookupList.DMSortOrder = new DMSortOrder();
                    }
                    SetDMPropertiesOnSortPropertyMapper();
                }
                else
                {
                    _lookupList = null;
                }
                _panelInfo.BusinessObject = _lookupList;
                _panelInfo.Panel.Enabled = hasProperty;
                ClassDef classDef = ClassDef.Get<DMLookupListBusinessObject>();
                Dictionary<string, string> lookupList1 = classDef.PropDefcol["ClassID"].LookupList.GetLookupList();
                ClassComboBoxMapper.LookupList = lookupList1;
            }
        }

        //public ICheckBox AscendingCheckBox
        //{
        //    get { return _ascendingCheckBox; }
        //}

        public DMLookupList LookupList
        {
            get { return _lookupList; }
        }

        public string LookupTypeName
        {
            get { return _businessObject; }
        }

        public IComboBox ClassComboBox
        {
            get { return _classComboBox; }
        }

        private void SetDMPropertiesOnSortPropertyMapper()
        {
            try
            {
                DMClass relatedClass = _lookupList.DMClass;
                if (relatedClass == null)
                {
                    _sortPropertySelectorMapper.DMProperties = null;
                }
                else
                {
                    _sortPropertySelectorMapper.DMProperties = relatedClass.AllProperties;
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        //public IBOComboBoxSelector SortPropertySelector
        //{
        //    get { return _sortByPropSelector; }
        //}
    }
}
